package com.company.cloud.goodsrestservice.web;

import com.company.cloud.goodsrestservice.core.Result;
import com.company.cloud.goodsrestservice.core.ResultGenerator;
import com.company.cloud.goodsrestservice.model.SkuLocation;
import com.company.cloud.goodsrestservice.service.SkuLocationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by reabout on 2018/04/01.
*/
@RestController
@RequestMapping("/sku/location")
public class SkuLocationController {
    @Resource
    private SkuLocationService skuLocationService;

    @PostMapping
    public Result add(@RequestBody SkuLocation skuLocation) {
        skuLocationService.save(skuLocation);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        skuLocationService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    public Result update(@RequestBody SkuLocation skuLocation) {
        skuLocationService.update(skuLocation);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        SkuLocation skuLocation = skuLocationService.findById(id);
        return ResultGenerator.genSuccessResult(skuLocation);
    }

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SkuLocation> list = skuLocationService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
